package com.me.bbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.me.bbs.anno.RequireLogin;
import com.me.bbs.common.Resp;
import com.me.bbs.common.SessionUtils;
import com.me.bbs.dto.ForumFollowingDTO;
import com.me.bbs.dto.PostDTO;
import com.me.bbs.entity.ForumFollowing;
import com.me.bbs.entity.Post;
import com.me.bbs.service.IForumFollowingService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lm
 * @since 2023-01-22
 */
@RestController
@RequestMapping("/forumFollowing")
public class ForumFollowingController {
    @Resource
    IForumFollowingService forumFollowingService;
    @PostMapping("/follow/{forumId}")
    @RequireLogin
    public Resp<?> follow(@PathVariable("forumId") Long forumId) throws Exception {
        Long uid = (Long) SessionUtils.getAttribute("user");
        ForumFollowing forumFollowing = new ForumFollowing();

        forumFollowing.setForum(forumId);
        forumFollowing.setUser(uid);
        forumFollowingService.follow(forumFollowing, true);
        return Resp.success();
    }

    @DeleteMapping("/follow/{forumId}")
    @RequireLogin
    public Resp<?> unfollow(@PathVariable("forumId") Long forumId) throws Exception {
        Long uid = (Long) SessionUtils.getAttribute("user");
        ForumFollowing forumFollowing = new ForumFollowing();
        forumFollowing.setForum(forumId);
        forumFollowing.setUser(uid);
        forumFollowingService.follow(forumFollowing, false);
        return Resp.success();
    }

    @PostMapping("/getFollowingForums")
    @RequireLogin
    public Resp<?> listByUser(@RequestParam("current") Long current,
                              @RequestParam("size") Long size) {
        Long uid = (Long) SessionUtils.getAttribute("user");
        LambdaQueryWrapper<ForumFollowing> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(uid != null,ForumFollowing::getUser, uid);
        Page<ForumFollowingDTO> forumFollowingPage = forumFollowingService
                .getForumFollowingList(
                        new Page<>(current,size)
                        , wrapper);
        return Resp.success(forumFollowingPage, "成功");
    }
}
